package com.epalmpay.controller.channel.order;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.order.GoodCouponVerifyInDTO;
import com.epalmpay.dto.order.GoodCouponVerifyListOutDTO;
import com.epalmpay.dto.order.GoodCouponVerifyOutDTO;
import com.epalmpay.dto.order.OrderDetailOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.service.order.IOrderService;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value="/channel/order")
public class GoodCouponVerifyController extends BaseController {
     
	@Resource  
    private IOrderService orderService;  


	/**
	 * 订单管理--商品券核销列表
	 * 
	 */
	@RequestMapping(value="/goodcouponverifyList")
	@ResponseBody
	public GoodCouponVerifyListOutDTO  GoodCouponVerifyList(GoodCouponVerifyInDTO goodCouponVerifyInDTO,PageDTO pageDTO){
		GoodCouponVerifyListOutDTO goodCouponVerifyListOutDTO = new GoodCouponVerifyListOutDTO();
		try {
		
			Page page=Tools.pageEmtity(pageDTO);	
			
            //查询时取数据			
			if(goodCouponVerifyInDTO.getIsReload()){
				//获取统计设备信息
				List<GoodCouponVerifyOutDTO> goodCouponVerifyOutDTOList = orderService.getGoodCouponVerifyList(goodCouponVerifyInDTO, page);
				goodCouponVerifyListOutDTO.setGoodCouponVerifyOutDTOList(goodCouponVerifyOutDTOList);
			}
			goodCouponVerifyListOutDTO.setPage(page); 
			goodCouponVerifyListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return goodCouponVerifyListOutDTO;
		} catch (ApplicationException ae) { 
			goodCouponVerifyListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		 catch (Exception e) { 
			 goodCouponVerifyListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return goodCouponVerifyListOutDTO;
	}		
		
	
	/**
	 * 订单管理--商品券码冲正
	 * 
	 */
	@RequestMapping(value="/goodcouponverifyCorrect")
	@ResponseBody
	public GoodCouponVerifyListOutDTO  GoodcouponverifyCorrect(GoodCouponVerifyInDTO goodCouponVerifyInDTO){
		GoodCouponVerifyListOutDTO goodCouponVerifyListOutDTO = new GoodCouponVerifyListOutDTO();
		try {
		
			 int result = orderService.editGoodCouponVerifyCorrect(goodCouponVerifyInDTO);
			 if(result == -1)
				throw new ApplicationException(Rescode.FAIL,"核销超过30分钟不能冲正！");
			 
			 goodCouponVerifyListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return goodCouponVerifyListOutDTO;
		} catch (ApplicationException ae) { 
			goodCouponVerifyListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			//ae.printStackTrace();
			logger.error(ae);
		}
		 catch (Exception e) { 
			 goodCouponVerifyListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return goodCouponVerifyListOutDTO;
	}		

	/**
	 * 订单管理--商品券码核销
	 * 
	 */
	@RequestMapping(value="/goodcouponverify")
	@ResponseBody
	public GoodCouponVerifyListOutDTO  Goodcouponverify(GoodCouponVerifyInDTO goodCouponVerifyInDTO){
		GoodCouponVerifyListOutDTO goodCouponVerifyListOutDTO = new GoodCouponVerifyListOutDTO();
		try {
		
			 int result = orderService.editGoodCouponVerify(goodCouponVerifyInDTO,ROOT_HOME_URL);
			 
			 goodCouponVerifyListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return goodCouponVerifyListOutDTO;
		} catch (ApplicationException ae) { 
			goodCouponVerifyListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			//ae.printStackTrace();
			logger.error(ae);
		}
		 catch (Exception e) { 
			 goodCouponVerifyListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return goodCouponVerifyListOutDTO;
	}		
	
	
	/**
	 * 订单管理--商品券码查找
	 * 
	 */
	@RequestMapping(value="/goodcouponverifySearchByCouponCode")
	@ResponseBody
	public OrderDetailOutDTO  GoodcouponverifySearchByCouponCode(GoodCouponVerifyInDTO goodCouponVerifyInDTO){
		OrderDetailOutDTO orderDetailOutDTO = new OrderDetailOutDTO();
		try {
		
			 orderDetailOutDTO = orderService.SearchOrderByCouponCode(goodCouponVerifyInDTO);
			 
			 orderDetailOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return orderDetailOutDTO;
		} catch (ApplicationException ae) { 
			orderDetailOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			logger.error(ae);
		}
		 catch (Exception e) { 
			 orderDetailOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return orderDetailOutDTO;
	}		
	
	
}
